<?php

namespace Live\Controller;
require_once 'Qiniu/autoload.php';
use \Qiniu\Auth;
use \Qiniu\Storage\UploadManager;
use Live\Controller\CommonController;

class ThemeController extends CommonController{

    public function theme_list(){
        $where['delete_time']=0;

        $themeList=M('FrontTheme')->where($where)->order('sort asc,id desc')->select();
        $this->assign('themeList',$themeList);
        $this->display();
    }

    public function theme_detail_list(){
        $id=I('get.cid');
        if($id){
            $cateInfo=M('FrontCategory')->find($id);
            if($cateInfo){
                $where['category_id']=$id;
                $themeList=M('Publish')
                    ->alias('p')
                    ->field('p.sort,t.*')
                    ->join('__FRONT_THEME__ t on p.theme_id=t.id')
                    ->where($where)->order('p.sort asc,p.update_time desc')->select();

                $this->assign('cateInfo',$cateInfo);
                $this->assign('themeList',$themeList);
                $this->display();
            }else{
                $this->error('栏目不存在！','/');
            }

        }else{
            $this->error('参数错误！',U('theme_list'));
        }

    }

    public function themeSetting(){
        $id=I('get.id');
        if($id){
            $themeInfo=M('FrontTheme')->find($id);
            if($themeInfo){
                //七牛对象存储的上传凭证
                $putPolicy=array(
                    'saveKey'=>'ThemeList/$(etag)',
                );
                $upAuth = new Auth(C('QINIU_accessKey'), C('QINIU_secretKey'));
                $bucket = C('QINIU_BUCKET');
                $uploadToken = $upAuth->uploadToken($bucket,null, 3600,$putPolicy);

                $this->assign('uploadToken',$uploadToken);
                $this->assign('themeInfo', $themeInfo);
                $this->display();
            }else{
                $this->error('此专题不存在',U('column_list'));
            }
        }else{
            $this->error('参数错误',U('column_list'));
        }

    }

    public function updateThemeSort(){
        $ids=I('post.ids');
        if($ids){
            $ids=explode(",",$ids);
            $time=time();
            M()->startTrans();
            foreach($ids as $k=>$v){
                $res=M('FrontTheme')->save(array('id'=>$v,'sort'=>$k,'update_time'=>$time));
                $mess['sql'][]=$res.":".M()->_sql();
                if($res===false){
                    M()->rollback();
                    $mess['resultCode']=1;
                    $mess['info']='保存失败';

                    $this->ajaxReturn($mess);
                    eixt();
                }
            }
            M()->commit();
            $mess['resultCode']=0;
            $mess['info']='';
        }else{
            $mess['resultCode']=1;
            $mess['info']='参数错误';
        }
        $this->ajaxReturn($mess);
    }

    public function saveThemeSet(){
        if(IS_POST){
            $data=I('post.');
            if(!isset($data['status'])){
                $data['status']=0;
            }
            $data['update_time']=time();
            foreach($data as $k=>$v){
                if($v==='' && $k!='content'){
                    unset($data[$k]);
                }
            }
            $res=M('FrontTheme')->save($data);
            if($res){
                $this->success("修改成功",U('theme_list'));
            }else{
                $this->error('修改失败，稍后重试');
            }
        }

    }

    public function addNewTheme(){
        if(IS_POST){
            $data=I('post.');
            $data['create_time']=$data['update_time']=time();
            $res=M('FrontTheme')->add($data);
            if($res){
                $this->success("添加成功",U('theme_list'));
            }else{
                $this->error('添加失败，稍后重试');
            }
        }else{
            //七牛对象存储的上传凭证
            $putPolicy=array(
                'saveKey'=>'columnList/$(etag)',
            );
            $upAuth = new Auth(C('QINIU_accessKey'), C('QINIU_secretKey'));
            $bucket = C('QINIU_BUCKET');
            $uploadToken = $upAuth->uploadToken($bucket,null, 3600,$putPolicy);


            $this->assign('uploadToken', $uploadToken);
            $this->display();
        }
    }

    public function delTheme(){
        $id=I('get.id');
        if($id){
            $data['delete_time']=time();
            $data['id']=$id;
            $res=M('FrontTheme')->save($data);
            if($res){
                $mess['resultCode']=0;
                $mess['info']='删除成功';
            }else{
                $mess['resultCode']=1;
                $mess['info']='删除失败，稍后重试';
            }
        }else{
            $mess['resultCode']=1;
            $mess['info']='参数错误';
        }
        $this->ajaxReturn($mess);
    }

    public function themeInfo(){
        $id=I('get.tid');
        if($id){
            $list=M('FrontThemeRecord')->where(array('theme_id'=>$id))->order('sort asc,update_time desc')->select();
            if($list){
                $cateInfo=M('FrontCategory')->find($id);
                $arr=array();
                foreach($list as $k=>$v){
                     if($v['live_group_id']!=""){

                         $res=M('Live')->field('title name,image_url')->where(array('group_id'=>$v['live_group_id']))->find();
                         if($res[''])
                         $arr[$k]['url']=U('Live/liveInfo',array('group_id'=>$v['live_group_id']));
                     }else if($v['article_id']!=0){
                         $arr[$k]=M('Live')->field('title name,image_url')->where(array('aid'=>$v['article_id']))->find();
                         $arr[$k]['url']=U('article/articleInfo',array('article_id'=>$v['article_id']));
                     }else if($v['s_theme_id']!=0){
                         $arr[$k]=M('FrontTheme')->field('name,image_url')->where(array('id'=>$v['s_theme_id']))->find();
                         $arr[$k]['url']=U('themeInfo',array('tid'=>$v['s_theme_id']));
                     }


                }

                $where['category_id']=$id;
                $this->assign('cateInfo',$cateInfo);
                $this->assign('contentList',$arr);
                $this->display();
            }else{
                $this->error('栏目不存在！','/');
            }

        }else{
            $this->error('参数错误！',U('theme_list'));
        }
    }
}